
package com.epam.android.training.app.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.Random;

/**
 * Class for creating/updading test's dsb. Including some initialization data
 * 
 * @author Natallia_Kukharchyk
 */
public class TestsOpenHelper extends SQLiteOpenHelper {
  private static final int DATABASE_VERSION = 14;

  private static final String DATABASE_NAME = "testsdb";

  public static final String TESTS_TABLE_NAME = "tests";

  public static final String QUESTIONS_TABLE_NAME = "questions";

  public static final String ANSWERS_TABLE_NAME = "answers";

  /* test fields */
  public static final String TEST_ID_FIELD = "test_id";

  public static final String TITLE_FIELD = "title";

  public static final String DESCRIPTION_FIELD = "description";

  public static final String IS_NEW_FIELD = "is_new";

  public static final String LAST_SCORE_FIELD = "last_score";

  private static final String TESTS_TABLE_CREATE = "CREATE TABLE " + TESTS_TABLE_NAME + " (" + TEST_ID_FIELD
      + " integer primary key autoincrement," + TITLE_FIELD + " text not null," + DESCRIPTION_FIELD + " text not null,"
      + IS_NEW_FIELD + " integer not null," + LAST_SCORE_FIELD + " long);";

  /* test's questions fields */
  public static final String QUESTION_ID_FIELD = "question_id";

  public static final String QUESTION_TITLE_FIELD = "question_title";

  public static final String RIGHT_ANSWER_FIELD = "right_answer";

  private static final String QUESTIONS_TABLE_CREATE = "CREATE TABLE " + QUESTIONS_TABLE_NAME + " ("
      + QUESTION_ID_FIELD + " integer primary key autoincrement," + TEST_ID_FIELD + " integer not null,"
      + QUESTION_TITLE_FIELD + " text not null," + RIGHT_ANSWER_FIELD + " integer not null);";

  /* answers table */
  public static final String ANSWER_ID_FIELD = "ansewr_id";

  public static final String ANSWER_TITLE_FIELD = "answer_title";

  private static final String ANSWERS_TABLE_CREATE = "CREATE TABLE " + ANSWERS_TABLE_NAME + " (" + ANSWER_ID_FIELD
      + " integer primary key autoincrement," + QUESTION_ID_FIELD + " integer not null," + ANSWER_TITLE_FIELD
      + " text not null);";

  private static final int ANSWERS_MAX_COUNT = 7;

  private static final int QUESTIONS_COUNT = 7;

  public TestsOpenHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);

  }

  @Override
  public void onCreate(SQLiteDatabase db) {
    db.execSQL(TESTS_TABLE_CREATE);
    db.execSQL(QUESTIONS_TABLE_CREATE);
    db.execSQL(ANSWERS_TABLE_CREATE);
    insertDefaultData(db, 5, QUESTIONS_COUNT, ANSWERS_MAX_COUNT);

  }

  /**
   * init some tests data
   * 
   * @param db
   */
  public void insertDefaultData(SQLiteDatabase db, int testNum, int questionsNum, int answNum) {
    insertTests(db, testNum, questionsNum, answNum, TITLE_FIELD);
  }

  /**
   * @param db
   * @param testNum
   * @param questionsNum
   * @param answNum
   * @param testTitle
   */
  public void insertTests(SQLiteDatabase db, int testNum, int questionsNum, int answNum, String testTitle) {
    for (int i = 0; i <= testNum; i++) {
      ContentValues values = new ContentValues();
      values.put(TITLE_FIELD, testTitle + i);
      values.put(DESCRIPTION_FIELD, DESCRIPTION_FIELD + i);
      values.put(IS_NEW_FIELD, 1);
      long test_id = db.insert(TestsOpenHelper.TESTS_TABLE_NAME, null, values);
      for (int j = 0; j <= questionsNum; j++) {
        ContentValues question = new ContentValues();
        question.put(QUESTION_TITLE_FIELD, testTitle + QUESTION_TITLE_FIELD + j);
        question.put(RIGHT_ANSWER_FIELD, 2);
        question.put(TEST_ID_FIELD, test_id);
        long question_id = db.insert(QUESTIONS_TABLE_NAME, null, question);
        int num = new Random().nextInt(answNum) + 2;
        for (int k = 0; k <= num; k++) {
          ContentValues answer = new ContentValues();
          answer.put(ANSWER_TITLE_FIELD, ANSWER_TITLE_FIELD + question_id + "-" + k);
          answer.put(QUESTION_ID_FIELD, question_id);
          db.insert(ANSWERS_TABLE_NAME, null, answer);
        }
      }
    }
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TESTS_TABLE_NAME);
    db.execSQL("DROP TABLE IF EXISTS " + QUESTIONS_TABLE_NAME);
    db.execSQL("DROP TABLE IF EXISTS " + ANSWERS_TABLE_NAME);
    onCreate(db);
  }
}

